package com.kunkun;

public class Random {
    private MainFrame mainFrame;
    private int[][] imageArray;
    public Random(MainFrame mainFrame,int[][] imageArray)
    {
        this.mainFrame = mainFrame;
        this.imageArray = imageArray;
        initRandomArray();
    }
    private void initRandomArray() {
        // 打乱二维数组的数字顺序
        for (int i = 0; i < imageArray.length; i++) {
            for (int j = 0; j < imageArray[i].length; j++) {
                // 随机两个行列位置，让这两个位置的数字交换
                int index1 = (int) (Math.random() * imageArray.length);
                int index2 = (int) (Math.random() * imageArray[i].length);
                int temp = imageArray[index1][index2];
                imageArray[index1][index2] = imageArray[i][j];
                imageArray[i][j] = temp;
            }
        }
        // 定位空白色块的位置
        kunkun:
        for (int i = 0; i < imageArray.length; i++) {
            for (int j = 0; j < imageArray[i].length; j++) {
                if (imageArray[i][j] == 0) {
                    mainFrame.setRow(i);
                    mainFrame.setCol(j);
                    break kunkun;
                }
            }
        }
    }
}
